/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"
#include "icamera.h"

#include "../resource.h"
#include "../kernel.h"

#include "SceneManager.h"
#include "irender.h"
#include "camera.h"

extern Kernel* g_kernel;
/**
============================
Constructor
============================
*/
SceneManager::SceneManager(void)
{ 
    m_currentScene = NULL;

    // register the cmd
    g_kernel->GetConsole()->AddCommand( &map_f, "map_change" );
    map_f.sceneMng = this;
}

/**
============================
Load a scene into memory
============================
*/
bool  SceneManager::LoadScene( const std::string &file )
{
	// free old scene
	UnloadScene();

    g_kernel->LPrintf( ("Attempting to load: " +file +"...").c_str() );
	// load new scene
    m_currentScene = m_importer.CreateScene( file );
    return (m_currentScene == NULL);
}
/**
============================
Unload a scene from memory
============================
*/
void SceneManager::UnloadScene()
{
    if ( m_currentScene ) {
		m_currentScene->FreeScene();
    }
}
/**
============================
Update the scene
============================
*/
void SceneManager::Update(long dt)
{

}
/**
============================
Draw the scene
============================
*/
void SceneManager::Draw(IRender* r, ICamera* cam)
{
    if ( m_currentScene ) {
        m_currentScene->Draw(r, cam );
    }
}



/**
============================
Deconstructor
============================
*/
SceneManager::~SceneManager(void)
{
	UnloadScene();
	// delete the scene
	if ( m_currentScene ) {
		delete m_currentScene;
		m_currentScene = NULL;
	}

    // unregister the cmd
    g_kernel->GetConsole()->RemoveCommand( "map_change" );
}




/*-----------------------------------------------------------------------------------------------------------------
    Map Change command
------------------------------------------------------------------------------------------------------------------*/

void MapChange_f::Exec(std::string &s)
{
    if ( sceneMng )
        // attemp to load the scene
        sceneMng->LoadScene( s );
}